package io.kiki.task.common.core.util;

import io.kiki.task.common.core.context.SpringContext;
import org.springframework.core.env.Environment;

import java.util.Objects;

/**
 * 获取环境信息
 */
public class EnvironmentUtils {
    public static final String DEFAULT_ENV = "default ";

    /**
     * 获取环境
     *
     * @return DEV、FAT、UAT、PROD
     */
    public static String getActiveProfile() {

        Environment environment = SpringContext.getBean(Environment.class);
        if (Objects.isNull(environment)) {
            return DEFAULT_ENV;
        }

        String[] activeProfiles = environment.getActiveProfiles();
        if (activeProfiles.length == 0) {
            return DEFAULT_ENV;
        }

        StringBuilder envs = new StringBuilder();
        for (String activeProfile : activeProfiles) {
            envs.append(activeProfile).append(" ");
        }
        return envs.toString();
    }

}
